//
// Created by dukang wang on 2024/12/19.
//

#ifndef HYAUDIO_AUDIORECORDER_H
#define HYAUDIO_AUDIORECORDER_H

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <string.h>
#include <string>
#include <jni.h>
#include "../AndroidLog.h"
#include <sys/syscall.h>
#include <unistd.h>

#define NUM_RECORDER_EXPLICIT_INTERFACES 2

using namespace std;

namespace hy {

    class AudioRecorder {

    private:
        SLEngineItf engineEngine = NULL;
        SLObjectItf engineObject = NULL;
        SLObjectItf recorderObject = NULL;//录制对象，这个对象我们从里面获取2个接口
        SLAndroidConfigurationItf configItf = NULL;
        SLAndroidSimpleBufferQueueItf recorderBuffQueueItf = NULL; //Buffer接口


    private:
        string AudioSrcPath = "";
        SLuint32 numBufferQueue = 1;
        int sampleRate = 44100;
        int numRecorderExplicitInterfaces = 2;
        int periodTime = 20; //20ms
        int frameSize = sampleRate * periodTime / 1000;//帧大小
        SLuint32 channels = 2;
        int mBufferSize = frameSize * channels;

    public:
        uint8_t *buffer;
        FILE *pfile;
        size_t bufferSize;
        SLRecordItf recorderRecord = NULL;

    private:
        AudioRecorder();

    public:
        ~AudioRecorder();

    public:
        static AudioRecorder *CreateInstance();

        int Init();

        void startRecord(const std::string recordPath);

        void stopRecord();

        void release();

    };

} // hy

#endif //HYAUDIO_AUDIORECORDER_H
